﻿using NoteSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace NoteSystem
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 免登录验证
                if (Request.Cookies["account"] != null & Request.Cookies["pwd"] != null)
                {
                    string account = Request.Cookies["account"].Value;
                    string pwd = Request.Cookies["pwd"].Value;
                    DataSet ds = DbHelper.Query($@"select u.*,d.DistrictName as district_name  from users u inner join S_District d on d.DistrictID=u.district_id
                                           where u.account = '{account}' and u.pwd = '{pwd}'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        //4.存session
                        Session["User"] = ds.Tables[0].Rows[0];//把当前登录人的整行字段信息全部存到session
                                                               //5.跳页
                        Response.Redirect("Index.aspx");
                    }
                }
                #endregion
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            #region 登录
            string account = txtAccount.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            string code = Session["Code"].ToString();
            string userCode = txtCode.Text.Trim();
            //验证码是否正确
            if (userCode != code)
            {
                Response.Write("<script>alert('验证码错误')</script>");
                return;
            }
            //账号密码是否正确
            DataSet ds = DbHelper.Query($@"select u.*,d.DistrictName as district_name  from users u inner join S_District d on d.DistrictID=u.district_id
                                           where u.account = '{account}' and u.pwd = '{pwd}'");
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('账号密码错误')</script>");
                return;
            }
            int type = Convert.ToInt32(ds.Tables[0].Rows[0]["type"]);
            if (type == 0)
            {
                Response.Write("<script>alert('请等待管理人员审核')</script>");
                return;
            }
            //验证码 账号密码 审核都是通过
            Session["User"] = ds.Tables[0].Rows[0];//将用户整行信息存入到Session
            if (cbxRMe.Checked)
            {
                HttpCookie accoutCookie = new HttpCookie("account");
                accoutCookie.Value = account;
                accoutCookie.Expires = DateTime.Now.AddMonths(1);
                HttpCookie pwdCookie = new HttpCookie("pwd");
                pwdCookie.Value = pwd;
                pwdCookie.Expires = DateTime.Now.AddMonths(1);
                Response.Cookies.Add(accoutCookie);
                Response.Cookies.Add(pwdCookie);
            }
            int id = Convert.ToInt32(ds.Tables[0].Rows[0]["id"]);
            string sql = string.Format("update users set login_count=login_count+1,last_login_time='{0}' where id={1}", DateTime.Now, id);
            //执行更新操作
            int flag = DbHelper.ExecuteSql(sql);
            if (flag > 0)
            {
                Response.Write("<script>alert('更新成功');window.location.href='03RepeaterItemCommand.aspx'</script>");
            }
            Response.Redirect("Index.aspx");
            #endregion
        }
    }
}